using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_MoveForum : Controls.DataManagerForumControl
{
    public event EventHandler ForumUpdated;
    protected override void ItemChanged()
    {
        RebindGrid();
    }

    protected void Page_Load()
    {
        if (!Page.IsPostBack)
        {
            RebindGrid();
        }
    }

    private void RebindGrid()
    {
        if (CurrentForum != null)
        {
            gridForums.DataSource = DAL.Forums.GetByParent(CurrentForum.Parent);
            gridForums.DataBind();
        }
        else
        {
            gridForums.DataSource = null;
            gridForums.DataBind();
        }
    }

    private DAL.Forum GetForum(object o)
    {
        if (o != null)
        {
            long forumID = 0;
            if (Int64.TryParse(o.ToString(), out forumID))
            {
                return DAL.Forums.Item(forumID);
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    private void UpdateForum(DAL.Forum forum, DAL.Direction dir)
    {
        DAL.Forums.Move(forum, dir);
        RebindGrid();

        if (ForumUpdated != null)
        {
            ForumUpdated(this, new EventArgs());
        }
    }

    protected void MoveForumUp(object sender, CommandEventArgs e)
    {
        UpdateForum(GetForum(e.CommandArgument), DAL.Direction.Up);
    }
    protected void MoveForumDown(object sender, CommandEventArgs e)
    {
        UpdateForum(GetForum(e.CommandArgument), DAL.Direction.Down);
    }
}
